package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.domain.vo.UserInfoVo;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 注册登录-控制层
 */
@RestController
@RequestMapping("/user")
public class LoginController {

    //注入业务逻辑处理对象 调用服务+编写业务逻辑代码
    @Autowired
    private UserService userService;

    /**
     * 保存用户返回userId
     * ResponseEntity:响应实体对象
     */
    @RequestMapping(value = "/saveUser", method = RequestMethod.POST)
    public ResponseEntity saveUser(@RequestBody User user) {
        Long userId = userService.saveUser(user);
        return ResponseEntity.ok(userId);
    }

    /**
     * 根据手机号码查询用户对象
     */
    @RequestMapping(value = "/findByMobile", method = RequestMethod.GET)
    public ResponseEntity findByMobile(String mobile) {
        User user = userService.findByMobile(mobile);
        return ResponseEntity.ok(user);
    }

    /**
     * 登录第一步---获取验证码
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseEntity sendValidCode(@RequestBody Map<String, String> params) {
        String mobile = params.get("phone");///获取手机号码
        userService.sendValidCode(mobile);
        return ResponseEntity.ok(null);
    }

    /**
     * 登录第二步---注册登录
     */
    @RequestMapping(value = "/loginVerification", method = RequestMethod.POST)
    public ResponseEntity loginReg(@RequestBody Map<String, String> params) {
        String mobile = params.get("phone");///获取手机号码
        String code = params.get("verificationCode");///获取验证码
        Map map = userService.loginReg(mobile,code);
        return ResponseEntity.ok(map);
    }

    /**
     * 设置头像-保存用户基础信息
     */
    @RequestMapping(value = "/loginReginfo", method = RequestMethod.POST)
    public ResponseEntity loginReginfo(@RequestBody UserInfoVo userInfoVo) {
        userService.loginReginfo(userInfoVo);
        return ResponseEntity.ok(null);
    }


    /**
     * 拍照-人脸识别上传头像
     * 方式一：MultipartFile headPhoto（名称不能随便写 必须和app请求一致）：接收文件对象
     * 方式二：@RequestParam("headPhoto") MultipartFile xxxx
     */
    @RequestMapping(value = "/loginReginfo/head", method = RequestMethod.POST)
    public ResponseEntity loginReginfoHead(MultipartFile headPhoto) {
        userService.loginReginfoHead(headPhoto);
        return ResponseEntity.ok(null);
    }

}
